﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Web.UI;

namespace yncTechWeb.Controls
{
    /// <summary>
    ///MailMenu 的摘要说明
    /// </summary>
    public class MainMenu : DataBoundControl
    {
        private struct MenuEntity
        {
            public string Text;
            public string URL;
            public decimal ParentID;
            public int Sequence;
            public decimal ID;
        }

        private List<MenuEntity> menuMainItems = new List<MenuEntity>();

        const string menuStart = @"<div id='{0}' style='width: 100%;padding-top:2px;padding-bottom:10px;'>
                <ul id='Ul1'>";

        const string menuEnd = @"                </ul>
            </div>";
        const string menuItemStart = @"<li itemtype='MainItem'><span class='m-link'>[Text]</span>
                        <ul id='defaultItem' style='display:block;'>";
        const string menuItemEnd = @"                        </ul>
                    </li>";
        const string menuList = "                            <li class='l-link' tabid='{0}' url='{1}'><span>{2}</span></li>";


        [DefaultValue("Menu")]
        public string MenuID
        {
            get;
            set;
        }

        private string menuText = "FunctionName";
        private string menuURL = "FunctionURL";
        private string menuParentID = "ParentID";
        private string menuSequence = "Sequence";
        private string menuFunctionID = "FunctionID";
        public string MenuFunctionID
        {
            get
            {
                return menuFunctionID;
            }
            set
            {
                menuFunctionID = value;
            }
        }
        public string MenuTextField
        {
            get
            {
                return menuText;
            }
            set
            {
                menuText = value;
            }
        }

        public string MenuURLField
        {
            get
            {
                return menuURL;
            }
            set
            {
                menuURL = value;
            }
        }

        public string MenuParentIDField
        {
            get
            {
                return menuParentID;
            }
            set
            {
                menuParentID = value;
            }
        }

        public string MenuSequenceField
        {
            get
            {
                return menuSequence;
            }
            set
            {
                menuSequence = value;
            }
        }

        public override object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                if ((value is IList) || (value is DataTable))
                {
                    base.DataSource = value;
                }
                //else
                //{
                //    throw new Exception("The DataSource should be DataTable or IList datasource.");
                //}
            }
        }

        protected override void PerformDataBinding(IEnumerable data)
        {
            if (data != null)
            {
                foreach (var item in data)
                {
                    MenuEntity menuEntity = new MenuEntity();
                    menuEntity.Text = Convert.ToString(DataBinder.GetPropertyValue(item, MenuTextField));
                    menuEntity.URL = Convert.ToString(DataBinder.GetPropertyValue(item, MenuURLField)).Trim();
                    menuEntity.ParentID = Convert.ToDecimal(DataBinder.GetPropertyValue(item, MenuParentIDField));
                    menuEntity.Sequence = Convert.ToInt32(DataBinder.GetPropertyValue(item, MenuSequenceField));
                    menuEntity.ID = Convert.ToDecimal(DataBinder.GetPropertyValue(item, MenuFunctionID));
                    menuMainItems.Add(menuEntity);
                }
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
           // base.Render(writer);

            writer.Write(string.Format(menuStart, MenuID ?? "Menu"));

            var entitys = menuMainItems.Where<MenuEntity>(item => item.ParentID < 0);

            foreach (var item in entitys)
            {
                writer.Write(menuItemStart.Replace("[Text]", item.Text));
                var subItems = menuMainItems.Where<MenuEntity>(subItem => subItem.ParentID == item.ID);
                foreach (var subItem in subItems)
                {
                    writer.Write(string.Format(menuList, subItem.ID, subItem.URL, subItem.Text));
                }
                writer.Write(menuItemEnd);
            }

            writer.Write(menuEnd);

        }
    }
}